package com.open.capacity.form.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.form.dao.FormDbFieldDao;
import com.open.capacity.form.model.FormDbField;
import com.open.capacity.form.service.FormDbFieldService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FormDbFieldServiceImpl implements FormDbFieldService {

    @Autowired
    private FormDbFieldDao dbFieldDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(FormDbField dbField) {
        dbField.setCreateTime(new Date());

        dbFieldDao.save(dbField);
        log.info("保存数据表常用字段：{}", dbField);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(FormDbField dbField) {

        dbFieldDao.updateByOps(dbField);
        log.info("修改数据表常用字段：{}", dbField);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        FormDbField dbField = dbFieldDao.findById(id);

        dbFieldDao.delete(id);

        log.info("删除数据表常用字段：{}", dbField);
    }


    @Override
    public FormDbField findById(String id) {
        return dbFieldDao.findById(id);
    }

    @Override
    public List<FormDbField> findDbFields(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<FormDbField> list = dbFieldDao.findList(params);
        return list;
    }


    @Override
    public Result saveOrUpdate(FormDbField dbField, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            FormDbField model = dbFieldDao.findById(keyValue);
            if (model != null) {
                dbField.setId(keyValue);
                i = dbFieldDao.updateByOps(dbField);
            }
        } else {
            dbField.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            dbField.setCreateTime(new Date());
            i = dbFieldDao.save(dbField);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
